<template>
  <van-tree-select :active-id="activeId" v-model:main-active-index="activeIndex" :items="items"
    :style="{height:'100%', paddingBottom:'50px'}" @click="selectedFunc">
  </van-tree-select>
</template>

<script setup lang="ts">
import { getDepList, getDerivedDepList } from '@/api/api';
import axios from 'axios';
import { onMounted, ref, watch } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { useStore } from 'vuex';
// import {insertDepList} from "../../api/api";
import $bus from "../../config/eventBus";
const store = useStore()
// const activeIndex = ref(0);
// const items = [{ text: '分组 1' }, { text: '分组 2' }]
const activeId = ref(1);
const activeIndex = ref(0);
const items = ref([])
const depList = ref([])
const router = useRouter()
const route = useRoute()
const tempArr1 = ref([])
const tempArr = ref([]);

(async () => {
  // const res = await getDepList()
  // onMounted是在setup之后再运行的
  // depList.value = res.data.result.data.map(item => ({ text: item.name, id: item.id }))
  // console.log(depList.value);

  // 获取父科室
  const depRes = await getDepList()

  depList.value = depRes.data.result.data
  // console.log('888', depList.value);
  store.state.depid = depList.value

  // onMounted是在setup之后再运行的
  // depList.value = res.data.data.map(item => ({ text: item.name, id: item.id }))
  // depList.value = res.data.data
  // console.log(depList.value);

  // const abc = await insertDepList()
  // console.log(abc);

  depList.value.forEach(async (item, index) => {
    const res = await getDerivedDepList(item.depid)
    tempArr1.value.push(res.data.result.data)
    if (index === depList.value.length - 1) {
      tempArr.value = tempArr1.value
    }
  })
})()


watch(
  () => tempArr.value,
  () => {
    depList.value.forEach((item) => {
      const arr = tempArr.value.find((i, idx) => {
        if (i.length && i[0].depid === item.depid) {
          return i
        }
      })
      items.value.push({
        text: item.name,
        children: arr ? arr.map(el => {
          if (el) {
            return { text: el.name, id: el.derid }
          }
        }) : []
      })
    })
  }
)

// watch(
//   () => tempArr.value,
//   () => {
//     depList.value.forEach((item) => {
//       items.value.push({
//         text: item.name,
//         children: tempArr.value.find(i => {
//           if (i.length) {
//             return i[0].depid === item.depid
//           } else {
//             return []
//           }
//         }).map(item => ({ text: item.name, id: item.derid }))
//       })
//     })
//   }
// )

// 用于首页点击热门科室跳转后定位到相应科室
$bus.on('selectDepartment', id => {
  activeIndex.value = id as number
})

// 选中时调用的函数
const selectedFunc = (e) => {
  const type = route.query.type
  if (e.target.className.includes("van-ellipsis")) {
    let dep = null
    for (let i = 0; i <= items.value.length; i++) {
      if (items.value[i].children.find(item => item.text === e.target.innerText)) {
        dep = items.value[i].text
        // 存储点击的父科室名
        localStorage.setItem('dep', dep)
        console.log(222, dep);
        console.log('aa',store.state.depid[i].depid);
        store.state.id=store.state.depid[i].depid

        break
      }
    }

    router.push({
      path: '/doctor',
      query: {
        name: encodeURI(dep),
        type
      }
    })
  }
}


console.log(new Date().toLocaleString());




/* 最后挂号时发送的请求
  axios.post(url, {
    visid:,
    depid:,
    clinicalitme:,
    price:,
    ordertime:
  }) 
*/


</script>